package com.conversationboard.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.conversationboard.model.Board;
import com.conversationboard.model.Boards;
import com.conversationboard.model.Colour;
import com.conversationboard.model.User;
import com.conversationboard.model.preferences.SiteNewPostsViewInfo;

@WebServlet(name = "CustomiseSiteNewPostsControllerServlet", urlPatterns = "/CustomiseSiteNewPostsControllerServlet")
public class CustomiseSiteNewPostsControllerServlet extends HttpServlet {

	private static final long serialVersionUID = 6050991880860731079L;


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		try {

			User user = User.get(request.getUserPrincipal().getName());
			Map<Integer, SiteNewPostsViewInfo> siteNewPostsViewInfo = user.getPreferences().getSiteNewPostsViewInfo();
			List<Board> boards = Boards.getAuthorisedBoards(user, false);

			request.setAttribute("colours", Colour.values());
			request.setAttribute("boards", boards);
			request.setAttribute("user", user);
			request.setAttribute("viewInfo", siteNewPostsViewInfo);

			RequestDispatcher dispatcher = request.getRequestDispatcher("/Pages/customise-site-new-posts.jsp");
			dispatcher.forward(request, response);

		} catch (SQLException e) {
			throw new ServletException(e);
		}

	}

}
